package cn.enilu.flash.api.controller.house;

import cn.enilu.flash.bean.entity.house.RoomInfo;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.house.RoomInfoService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;

import cn.enilu.flash.warpper.BuildingInfoWarpper;
import cn.enilu.flash.warpper.RoomInfoWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomInfoController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private RoomInfoService roomInfoService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list() {
	Page<RoomInfo> page = new PageFactory<RoomInfo>().defaultPage();
		page = roomInfoService.queryPage(page);
		List list = (List) new RoomInfoWarpper(BeanUtil.objectsToMaps(page.getRecords())).warp();
		page.setRecords(list);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑房间", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute RoomInfo roomInfo){
		if(roomInfo.getId()==null){
			roomInfoService.insert(roomInfo);
		}else {
			roomInfoService.update(roomInfo);
		}
		return Rets.success();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除房间", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		roomInfoService.delete(id);
		return Rets.success();
	}

	@RequestMapping(value = "/options/{bid}",method = RequestMethod.GET)
	public Object options(@PathVariable("bid") Long bid) {
		SearchFilter sf = SearchFilter.build("buildingId", SearchFilter.Operator.EQ, bid);
		return Rets.success(roomInfoService.queryAll(sf));
	}
}